/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.FeeBillBean;
import com.minierp.bean.FeeBillItemBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.BankAccountBalanceDao;
import com.minierp.dao.BankAccountDao;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.DepartmentDao;
import com.minierp.dao.FeeBillDao;
import com.minierp.dao.FeeBillItemDao;
import com.minierp.dao.FeeTypeDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.PayModeDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.CodeRule;
import com.minierp.model.EnterpriseInfo;
import com.minierp.model.FeeBill;
import com.minierp.model.FeeBillItem;
import com.minierp.model.TallySet;
import com.minierp.service.FeeBillService;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

public class FeeBillFormCtr extends SimpleFormController {
	private DepartmentDao departmentDao;
	private BankAccountDao bankAccountDao;
	private FeeBillDao feeBillDao;
	private FeeBillItemDao feeBillItemDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private BankAccountBalanceDao bankAccountBalanceDao;
	private PayModeDao payModeDao;
	private FeeTypeDao feeTypeDao;
	private BusinessBillDao businessBillDao;
	private IdGenerator idGenerator;
	private FeeBillService feeBillService;

	public FeeBillItemDao getFeeBillItemDao() {
		return this.feeBillItemDao;
	}

	public void setFeeBillItemDao(FeeBillItemDao feeBillItemDao) {
		this.feeBillItemDao = feeBillItemDao;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		String retVal = request.getParameter("ret_val");
		if (retVal == null) {
			retVal = "";
		}

		refData.put("retVal", retVal);
		refData.put("departmentList", this.departmentDao.getList(true));
		List baList = this.bankAccountDao.getList("", "");
		EnterpriseInfo ent = EncCommonHelper.getEnterpriseInfo(request);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		long dfTsId = 0L;
		long specId = 0L;
		if (id != null && !id.equals("")) {
			FeeBill bb = (FeeBill) this.feeBillDao.get(FeeBill.class,
					Long.parseLong(id));
			specId = bb.getBankAccountId();
			dfTsId = bb.getTallySetId();
		} else {
			dfTsId = ub.getUser().getDfTsId();
		}

		refData.put("bankAccountList",
				PageHelper.parseBaList(baList, ent, ub, dfTsId, specId));
		refData.put("feeTypeList", this.feeTypeDao.getList(false));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("FEE_BILL");
		if (cr == null) {
			throw new Exception("Code rule not found for fee bill");
		} else {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			FeeBillBean form = new FeeBillBean();
			form.setCodeRule(cr);
			FeeBillItemBean cb;
			FeeBillItem c;
			if (id != null && !id.equals("")) {
				FeeBill arg16 = (FeeBill) this.feeBillDao.get(FeeBill.class,
						Long.parseLong(id));
				if (arg16 == null) {
					throw new Exception("Record not found for id:" + id);
				}

				TallySet arg17 = (TallySet) this.feeBillDao.get(TallySet.class,
						arg16.getTallySetId());
				List arg18 = this.feeTypeDao.getList(false);
				List arg20 = this.feeBillItemDao.getList(arg16.getId());
				form.setSychronizeValue(arg16.getUpdateTime().getTime());
				if (arg17 != null) {
					form.setTallySetName(arg17.getTitle());
				}

				form.setOldId(arg16.getId());
				form.setFeeDateStr(sf.format(arg16.getFeeDate()));
				form.setFeeBill(arg16);
				if (arg16.getAttachFile() != null
						&& !arg16.getAttachFile().equals("")) {
					int arg22 = arg16.getAttachFile().lastIndexOf("/");
					form.setAttachFileName(arg16.getAttachFile().substring(
							arg22 + 1));
				}

				ArrayList arg23 = new ArrayList();

				for (int arg25 = 0; arg25 < arg20.size(); ++arg25) {
					cb = new FeeBillItemBean();
					c = (FeeBillItem) arg20.get(arg25);
					cb.setFeeBillItem(c);
					cb.setCostStr(c.getCost().toString());
					cb.setFtName(EncCommonHelper.getSimpleObjectName(arg18,
							c.getFtId()));
					cb.setOldId(c.getId());
					cb.setStatus("UNCHANGE");
					arg23.add(cb);
				}

				form.setItems(arg23);
			} else {
				if (!EncCommonHelper.checkPrivilege(request, "FEE_BILL")) {
					throw new PermissionDennyException("Permission Denny");
				}

				String templateId = request.getParameter("template_id");
				UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
				FeeBill feeBill = new FeeBill();
				if (templateId != null && !templateId.equals("")) {
					FeeBill arg19 = (FeeBill) this.feeBillDao.get(
							FeeBill.class, Long.parseLong(templateId));
					if (arg19 == null) {
						throw new Exception(
								"Template FeeBill not found for id "
										+ templateId);
					}

					feeBill.setBankAccountId(arg19.getBankAccountId());
					feeBill.setDepartmentId(arg19.getDepartmentId());
					feeBill.setFeeDate(arg19.getFeeDate());
					feeBill.setManagerId(arg19.getManagerId());
					feeBill.setManagerName(arg19.getManagerName());
					feeBill.setMark(arg19.getMark());
					feeBill.setPayModeId(arg19.getPayModeId());
					feeBill.setReceiverName(arg19.getReceiverName());
					List arg21 = this.feeTypeDao.getList(false);
					List arg24 = this.feeBillItemDao.getList(arg19.getId());
					ArrayList arg26 = new ArrayList();

					for (int arg27 = 0; arg27 < arg24.size(); ++arg27) {
						FeeBillItemBean cb1 = new FeeBillItemBean();
						FeeBillItem c1 = new FeeBillItem();
						FeeBillItem tc = (FeeBillItem) arg24.get(arg27);
						c1.setCost(tc.getCost());
						c1.setFtId(tc.getFtId());
						cb1.setFeeBillItem(c1);
						cb1.setCostStr(c1.getCost().toString());
						cb1.setFtName(EncCommonHelper.getSimpleObjectName(
								arg21, c1.getFtId()));
						cb1.setOldId(c1.getId());
						cb1.setStatus("NEW");
						arg26.add(cb1);
					}

					form.setItems(arg26);
				} else {
					feeBill.setManagerId(ub.getStaffer().getId());
					feeBill.setManagerName(ub.getStaffer().getStafferName());
					feeBill.setDepartmentId(ub.getStaffer().getDepartmentId());
					String bbCode = request.getParameter("bb_code");
					String bbId = request.getParameter("bb_id");
					ArrayList arr;
					if (bbId != null && !bbId.equals("")) {
						arr = new ArrayList();
						cb = new FeeBillItemBean();
						c = new FeeBillItem();
						c.setBbCode(bbCode);
						c.setBbId(Long.parseLong(bbId));
						cb.setFeeBillItem(c);
						cb.setCostStr(c.getCost().toString());
						cb.setOldId(c.getId());
						cb.setStatus("NEW");
						arr.add(cb);
						form.setItems(arr);
					} else {
						arr = new ArrayList();
						cb = new FeeBillItemBean();
						c = new FeeBillItem();
						cb.setFeeBillItem(c);
						cb.setCostStr("");
						arr.add(cb);
						form.setItems(arr);
					}
				}

				feeBill.setCreaterId(ub.getStaffer().getId());
				feeBill.setCreaterName(ub.getStaffer().getStafferName());
				feeBill.setTallySetId(ub.getUser().getDfTsId());
				form.setTallySetName(ub.getDfTsName());
				form.setFeeDateStr(sf.format(feeBill.getFeeDate()));
				form.setFeeBill(feeBill);
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		FeeBillBean form = (FeeBillBean) command;
		int num = Integer.parseInt(request.getParameter("item_num"));
		ArrayList arr = new ArrayList();

		for (int i = 0; i < num; ++i) {
			FeeBillItemBean cb = new FeeBillItemBean();
			FeeBillItem c = new FeeBillItem();
			cb.setCostStr(request.getParameter("cost_" + i));
			cb.setFtName(request.getParameter("ft_name_" + i));
			c.setBbCode(request.getParameter("bb_code_" + i));
			c.setBbId(Long.parseLong(request.getParameter("bb_id_" + i)));
			c.setFtId(Long.parseLong(request.getParameter("ft_id_" + i)));
			c.setMark(request.getParameter("mark_" + i));
			if (request.getParameter("id_" + i) != null
					&& !request.getParameter("id_" + i).equals("")) {
				c.setId(Long.parseLong(request.getParameter("id_" + i)));
				cb.setOldId(c.getId());
			}

			cb.setStatus(request.getParameter("status_" + i));
			cb.setFeeBillItem(c);
			arr.add(cb);
		}

		form.setItems(arr);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String id = request.getParameter("id");
		String examine = request.getParameter("examine");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		FeeBillBean form = (FeeBillBean) command;
		FeeBill obj = form.getFeeBill();
		if (!EncCommonHelper.checkPrivilege(request, "FEE_BILL")) {
			throw new Exception("Permission deny!");
		} else {
			if (examine != null && examine.equals("1")) {
				if (!EncCommonHelper.checkPrivilege(request, "FEE_EXAM")) {
					throw new Exception("Permission deny!");
				}

				obj.setExamined("Y");
				if (ub.getStaffer() != null) {
					obj.setExaminerId(ub.getStaffer().getId());
					obj.setExaminerName(ub.getStaffer().getStafferName());
				}
			} else {
				obj.setExamined("N");
			}

			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile multipartFile = multipartRequest
					.getFile("attach_file");
			String continueFlag;
			String alertType;
			String printFlag;
			if (multipartFile != null && !multipartFile.isEmpty()) {
				continueFlag = multipartFile.getOriginalFilename();
				alertType = EncCommonHelper.getExtendName(continueFlag);
				printFlag = EncCommonHelper.getUploadVirtualPath(this
						.getServletContext());
				String realPath = this.getServletContext().getRealPath(
						printFlag);
				String fileName = ub.getUser().getId() + "_"
						+ System.currentTimeMillis() + "." + alertType;
				FileCopyUtils.copy(multipartFile.getBytes(), new File(realPath
						+ System.getProperty("file.separator") + fileName));
				obj.setAttachFile(printFlag + fileName);
			}

			if (id != null && !id.equals("")) {
				try {
					this.feeBillService.txUpdate(form, this.feeBillDao,
							this.feeBillItemDao, this.bankAccountBalanceDao,
							this.businessBillDao, this.idGenerator);
				} catch (CannotAcquireLockException arg17) {
					EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
					return this.showForm(request, errors, this.getFormView());
				}
			} else {
				try {
					this.feeBillService.txInsert(form, this.feeBillDao,
							this.feeBillItemDao, this.bankAccountBalanceDao,
							this.businessBillDao, this.serialNumberDao,
							this.idGenerator);
				} catch (CannotAcquireLockException arg18) {
					EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
					return this.showForm(request, errors, this.getFormView());
				}
			}

			mv.addObject("returnValue", String.valueOf(obj.getId()));
			continueFlag = request.getParameter("continue_flag");
			alertType = request.getParameter("alert_type");
			printFlag = request.getParameter("print_flag");
			if (continueFlag != null && !continueFlag.equals("")) {
				if (continueFlag.equals("NEW")) {
					EncCommonHelper.setAlertMessage(request, "费用单保存成功！");
					mv = new ModelAndView(
							"redirect:/finance/FeeBillNew.ihtm?ret_val=1&alert_type="
									+ alertType);
				} else if (continueFlag.equals("COPY")) {
					EncCommonHelper.setAlertMessage(request, "费用单保存成功！");
					mv = new ModelAndView(
							"redirect:/finance/FeeBillNew.ihtm?ret_val=1&template_id="
									+ obj.getId() + "&alert_type=" + alertType);
				}
			} else if (printFlag != null && printFlag.equals("1")) {
				mv = new ModelAndView(
						"redirect:/finance/FeeBillPrint.ihtm?ret_val=1&id="
								+ obj.getId());
			} else if (alertType == null || !alertType.equals("NONE")) {
				EncCommonHelper.setAlertMessage(request, "费用单保存成功！");
				if (alertType != null && alertType.equals("FORM")) {
					mv = new ModelAndView(new RedirectView(
							"/finance/FeeBillEdit.ihtm?ret_val=1&id="
									+ obj.getId() + "&alert_type=" + alertType));
				}
			}

			mv.addObject("alertType", alertType);
			return mv;
		}
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public DepartmentDao getDepartmentDao() {
		return this.departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public BankAccountDao getBankAccountDao() {
		return this.bankAccountDao;
	}

	public void setBankAccountDao(BankAccountDao bankAccountDao) {
		this.bankAccountDao = bankAccountDao;
	}

	public FeeBillDao getFeeBillDao() {
		return this.feeBillDao;
	}

	public void setFeeBillDao(FeeBillDao feeBillDao) {
		this.feeBillDao = feeBillDao;
	}

	public FeeBillService getFeeBillService() {
		return this.feeBillService;
	}

	public void setFeeBillService(FeeBillService feeBillService) {
		this.feeBillService = feeBillService;
	}

	public BankAccountBalanceDao getBankAccountBalanceDao() {
		return this.bankAccountBalanceDao;
	}

	public void setBankAccountBalanceDao(
			BankAccountBalanceDao bankAccountBalanceDao) {
		this.bankAccountBalanceDao = bankAccountBalanceDao;
	}

	public FeeTypeDao getFeeTypeDao() {
		return this.feeTypeDao;
	}

	public void setFeeTypeDao(FeeTypeDao feeTypeDao) {
		this.feeTypeDao = feeTypeDao;
	}

	public PayModeDao getPayModeDao() {
		return this.payModeDao;
	}

	public void setPayModeDao(PayModeDao payModeDao) {
		this.payModeDao = payModeDao;
	}

	public BusinessBillDao getBusinessBillDao() {
		return this.businessBillDao;
	}

	public void setBusinessBillDao(BusinessBillDao businessBillDao) {
		this.businessBillDao = businessBillDao;
	}
}